#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Tue Jun 18 15:25:53 2019

@author: jili
"""
import datetime
import jili.price.kline as kline
from pymongo import MongoClient as mc
client_tick = mc("mongodb://ant:ant@localhost:27777/tick_f")
tickf=client_tick["tick_f"]
client_kline = mc("mongodb://ant:ant@localhost:27777/kline1d")
kline1d=client_kline["kline1d"]
start_date=datetime.datetime(2018,10,16,20,50,0)
cur= tickf["c_rb1910"].find({}).sort([("timekey",1)]).batch_size(5000)
kl=kline.kline1m("rb1910")
for i in cur:
    kl.ontick(i)
kline1d["kline1m"].create_index([("obj",1),("timekey", 1)], unique=True, background=True)
def insert_one(t):
    # 可以使用insert直接一次性向mongoDB插入整个列表，也可以插入单条记录，但是'_id'重复会报错
    try:
        kline1d["kline1m"].insert_one(t)
    except Exception as e:
        print(t["timekey"],e)
for i in kl.bar:
    insert_one(i)